package com.findMoneyGame.server {
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	import com.findMoneyGame.server.ServerBridge;
	import com.findMoneyGame.server.IServerFacade;
	import com.findMoneyGame.models.UserData;

	public class MockServerFacade implements IServerFacade {
		private static const timerCount = 4;
		private static const timerDelay = 50;

		private var server : ServerBridge = new ServerBridge();
		private var timer : Timer = new Timer(timerDelay, timerCount);
		private var userData : Object;

		public function get imageBytesTotal () : int { return server.imageBytesTotal; }
		public function get imageBytesLoaded () : int { return server.imageBytesLoaded; }
		public function get bytesTotal () : int { return timerCount; }
		public function get bytesLoaded () : int { return timer.currentCount; }


		public function MockServerFacade () {
			userData = new Object();
			userData.level = 59;
			userData.moneyCount = 453;
			userData.moneySpent = 789;
			userData.moneyBought = 400;
			userData.achievement = -1;
			userData.fastCompletedLevelCount = 2;
			userData.loupeBonusIndex = 0;
			userData.hintBonusIndex = 0;
			userData.timeBonusIndex = -1;
		}
		public function beginLoadImage (url : String, complete : Function, error : Function) {
			server.beginLoadImage(url, complete, error);
		}
		public function beginCheckUserExists (id : int, complete : Function, error : Function) {
			startTimer(function () { complete(true); });
		}
		public function beginCreateNewUser (id : int, complete : Function, error : Function) {
			startTimer(function () { complete(); });
		}
		public function beginFetchUserData (id : int, complete : Function, error : Function) {
			startTimer(function () { complete(userData); });
		}
		public function beginSaveUserData (savingData : UserData, complete : Function, error : Function) {
			trace('MockServerFacade.beginSaveUserData(): Начинаю сохранение данных пользователя на сервере');
			startTimer(complete);
		}
		public function beginFetchGameLevel (level : int, complete : Function, error : Function) {
			var reqComplete = function () {
				var data = new Object();
				data.reward = 10;
				data.bonusTimeReward = 15;
				data.passLevelPrice = 50;
				data.hintPrice = 50;
				data.loupePrice = 50;
				data.loupeTime = 10;
				data.hintTime = 10;
				data.failingPrice = 10;
				data.targetRectX = 0;
				data.targetRectY = 0;
				data.targetRectWidth = 100;
				data.targetRectHeight = 50;
				var photoNum = int(Math.random() * 5);
				data.imageUrl = 'server/images/photo' + (photoNum == 0 ? 1 : photoNum) + '.jpg';
				complete(data);
			};
			startTimer(reqComplete);
		}

		private function startTimer (listener) {
			var reqComplete = function (e : TimerEvent) { 
				removeTimerCompleteEventListener(reqComplete);
				listener();
			};
			addTimerCompleteEventListener(reqComplete);
			timer.start();
		}
		private function addTimerCompleteEventListener (listener) {
			timer.addEventListener(TimerEvent.TIMER_COMPLETE, listener);
		}
		private function removeTimerCompleteEventListener (listener) {
			timer.removeEventListener(TimerEvent.TIMER_COMPLETE, listener);
		}
		private function addTimerTickEventListener (listener) {
			timer.addEventListener(TimerEvent.TIMER, listener);
		}
		private function removeEventListener (listener) {
			timer.removeEventListener(TimerEvent.TIMER, listener);
		}
	}
}